package com.android.fragmentdemo.adbutil
import android.Manifest
import android.content.Context
import android.provider.Settings
import java.io.DataOutputStream
import java.io.OutputStream
import java.nio.charset.Charset


object ADBUtil {
    //应用授权
    fun grantPermission(
        pkgName: String,
        permission: String = Manifest.permission.WRITE_SECURE_SETTINGS
    ) {
        val command = "pm grant $pkgName $permission"
        baseAction(command)
    }

    //销毁进程
    fun killProcess(pkgName: String) {
        val command = "am force-stop $pkgName"
        baseAction(command)
    }

    //启动activity
    fun openActivity(pkgName: String, activityName: String) {
        val command = "am start -n ${pkgName}/${activityName}"
        baseAction(command)
    }

    //启动service
    fun openService(pkgName: String, serviceName: String) {
        val command = "am startservice -n ${pkgName}/${serviceName}"
        baseAction(command)
    }

    fun sendBroadcast(action: String) {
        val command = "am broadcast -a $action"
        baseAction(command)
    }

    //安装应用
    fun installApk(apkPath: String) {
        val command = "install -r -g -t $apkPath"
        baseAction(command)
    }

    //卸载应用
    fun uninstallApk(pkgName: String) {
        val command = "uninstall $pkgName"
        baseAction(command)
    }

    fun updateScreenSize() {
        val command = "adb shell wm size 540x1188"
        baseAction(command)
    }

    fun enableAccessibilityService(
        context: Context,
        pkgServiceFullName: String = "com.android.fragmentdemo/com.android.fragmentdemo.service.MonitorService"
    ) {
        Settings.Secure.putString(
            context.contentResolver,
            Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
            pkgServiceFullName
        )
        Settings.Secure.putInt(context.contentResolver, Settings.Secure.ACCESSIBILITY_ENABLED, 1)
    }

    fun disableAccessibilityService(context: Context,
                                    pkgServiceFullName: String = "com.android.fragmentdemo/com.android.fragmentdemo.service.MonitorService") {
        Settings.Secure.putString(
            context.contentResolver,
            Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
            pkgServiceFullName
        )
        Settings.Secure.putInt(context.contentResolver, Settings.Secure.ACCESSIBILITY_ENABLED, 0)
    }

    fun getAccessibilityServiceEnableFlag(context: Context) : Int {
        return Settings.Secure.getInt(
            context.contentResolver,
            Settings.Secure.ACCESSIBILITY_ENABLED,
            -1
        )
    }

    private fun baseAction(command: String) {
        var outputStream: OutputStream? = null
        var dataOutputStream: DataOutputStream? = null
        try {
            //su为root用户  sh为普通用户
            val process = Runtime.getRuntime().exec("su")
            outputStream = process.outputStream
            dataOutputStream = DataOutputStream(outputStream)
            dataOutputStream.writeBytes(command)
            //dataOutputStream.write(command.toByteArray(Charset.forName("utf-8")))
            dataOutputStream.writeChars("\n")
            dataOutputStream.flush()
            dataOutputStream.writeChars("exit")
            dataOutputStream.writeChars("\n")
            dataOutputStream.flush()
        } catch (ex: Exception) {
            ex.printStackTrace()
        } finally {
            dataOutputStream?.close()
            outputStream?.close()
        }
    }

}